<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="queryParams.username" :placeholder="'请输入账号'" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="queryParams.name" :placeholder="'请输入姓名'" style="width: 200px;margin-left: 10px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        添加
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      :empty-text="'无数据'"
      element-loading-text="加载中..."
      border
      fit
      highlight-current-row
    >
      <el-table-column prop="username" align="center" label="账号" />
      <el-table-column prop="name" align="center" label="姓名" />
      <el-table-column prop="sex" align="center" label="性别" />
      <el-table-column prop="phone" align="center" label="电话" />
      <el-table-column prop="email" align="center" label="邮件" />
      <el-table-column prop="createTime" align="center" label="创建时间" />
      <el-table-column prop="updateTime" align="center" label="更新时间" />
      <el-table-column :label="'操作'" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialogFormVisible" :title="textMap[dialogStatus]">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item :label="'账号'" prop="username">
          <el-input v-model="temp.username" :disabled="dialogStatus==='update'" :placeholder="'请输入账号'" @keyup.enter.native="dialogStatus==='create'?createData():updateData()" />
        </el-form-item>
        <el-form-item :label="'姓名'" prop="name">
          <el-input v-model="temp.name" :placeholder="'请输入姓名'" @keyup.enter.native="dialogStatus==='create'?createData():updateData()" />
        </el-form-item>
        <el-form-item :label="'电话'" prop="phone">
          <el-input v-model="temp.phone" :placeholder="'请输入电话'" @keyup.enter.native="dialogStatus==='create'?createData():updateData()" />
        </el-form-item>
        <el-form-item :label="'邮箱'" prop="email">
          <el-input v-model="temp.email" :placeholder="'请输入电话'" @keyup.enter.native="dialogStatus==='create'?createData():updateData()" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="temp.sex">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="roleSelect" multiple placeholder="请选择">
            <el-option
              v-for="item in roleOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          返回
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addUser, delUser, getUserList, updateUser } from '@/api/user'
import waves from '@/directive/waves'
import { getRoleOptionSelect } from '@/api/role'
import { getRoles } from '@/api/userrole'

export default {
  directives: { waves },
  data() {
    return {
      list: null,
      listLoading: true,
      queryParams: {
        username: '',
        name: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑用户',
        create: '添加用户'
      },
      temp: {
        username: '',
        name: '',
        sex: '',
        phone: '',
        email: ''
      },
      // 角色列表
      roleOptions: [],
      roleSelect:[],
      rules: {
        username: [{ required: true, message: '账号必填', trigger: 'blur' }],
        name: [{ required: true, message: '姓名必填', trigger: 'blur' }],
        phone: [{ required: true, message: '电话必填', trigger: 'blur' }],
        email: [{ required: true, message: '电话必填', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getUserList()
    this.getRoleOptionSelect()
  },
  methods: {
    handleFilter() {
      this.getUserList()
    },
    getUserList() {
      this.listLoading = true
      getUserList(this.queryParams).then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    getRoleOptionSelect(){
      getRoleOptionSelect().then(response => {
        this.roleOptions = response.data
      })
    },
    resetTemp() {
      this.temp = {
        username: '',
        name: '',
        sex: '',
        phone: '',
        email: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.roleSelect = []
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.roleSelect = []
      getRoles(this.temp.username).then(response => {
        this.roleSelect = response.data
      })
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delUser(row.id).then(response => {
          this.getUserList()
          this.$notify({
            title: '成功',
            message: '删除角色成功',
            type: 'success',
            duration: 2000
          })
        })
      }).catch(() => {
        this.$notify({
          title: '成功',
          message: '已取消删除',
          type: 'success',
          duration: 2000
        })
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addUser(this.temp).then(response => {
            this.getUserList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '添加用户成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          updateUser(tempData).then(response => {
            this.getUserList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '修改用户成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  }
}
</script>
